﻿using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Prismx.Cool.Extensions
{
    /// <summary>
    /// 遮罩效果
    /// </summary>
    public static class MaskExtensions
    {
        private static UIElement _waitControl;
        private static string _gridName;
        /// <summary>
        /// 初始化遮罩
        /// </summary>
        /// <param name="application"></param>
        /// <param name="element">等待效果控件</param>
        /// <param name="gridName">承载等待控件名称，类型为Grid</param>
        public static void UseMask(this Application application, UIElement element, string gridName)
        {
            _gridName = gridName;
            _waitControl = element;
        }
        /// <summary>
        /// 展示遮罩
        /// </summary>
        public static void Show()
        {
            try
            {
                Window win = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
                if (win == null)
                {
                    return;
                }
                Grid container = win.GetChildObject<Grid>(_gridName);
                if (container == null) throw new Exception($"界面上未找到名称为{_gridName}的Grid容器控件！");
                container.Children.Add(_waitControl);
            }
            catch
            {
            }
        }


        public static void Close()
        {
         
            try
            {
                Window win = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
                Grid container = win.GetChildObject<Grid>(_gridName);
                if (container == null) throw new Exception($"界面上未找到名称为{_gridName}的Grid容器控件！");
                container.Children.Remove(_waitControl);
            }
            catch
            {
            }
        }
    }
}
